import { useReducer } from 'react';

/**
 *
 * @SHORTCUT_CHECKKEY 菜单选中key
 * @CLEAR_RECEIVE 清空状态
 *
 */
export enum IShortcutActionEnum {
  SHORTCUT_CHECKKEY = 'shortcutCheckKey',
  MAILPHONE_SUCCESSKEY = 'mailOrPhoneSuccess',
  CLEAR_RECEIVE = 'clearAllReceive',
}

/**
 * 定义新增和编辑页面操作匹配
 * @type 操作类型
 * @payload 传递数据
 */
export interface IActionType {
  type: IShortcutActionEnum;
  payload?: any;
}

/**
 * 定义初始状态信息
 * @loading 加载标记
 */
export interface IstateType {
  shortcutCheckKey: string;
  mailOrPhoneSuccess: string;
}

const globalShortcutReducers = () => {
  /** 初始化状态 */
  const initState: IstateType = {
    shortcutCheckKey: '',
    mailOrPhoneSuccess: '',
  };

  const shortcutReducer = (state: IstateType, action: IActionType) => {
    switch (action?.type) {
      /**
       * 选中快捷方式key
       */
      case IShortcutActionEnum.SHORTCUT_CHECKKEY:
        return {
          ...state,
          shortcutCheckKey: action?.payload,
        };

      /**
       * 短信成功或者电话成功
       */
      case IShortcutActionEnum.MAILPHONE_SUCCESSKEY:
        return {
          ...state,
          mailOrPhoneSuccess: action?.payload,
        };

      /**
       * 关闭接收部门modal,清空已选数据
       */
      case IShortcutActionEnum.CLEAR_RECEIVE:
        return {
          ...state,
          shortcutCheckKey: '',
          mailOrPhoneSuccess: '',
        };

      default:
        return {
          ...state,
          ...initState,
        };
    }
  };

  const [state, shortcutDispatch] = useReducer(shortcutReducer, initState);

  return {
    ...state,
    shortcutDispatch,
  };
};
export { globalShortcutReducers };
